package cave;

import java.util.ArrayList;
import android.media.AudioManager;
import android.media.SoundPool;
import android.media.SoundPool.OnLoadCompleteListener;
import cave.engine.R;
import cave.game.GameServices;
import cave.game.InputEvent;
import cave.game.MainActivity;
import cave.game.Screen;
import cave.layer.Layer;
import cave.renderer.RenderList;
import cave.ui.Image;
import cave.ui.UiContainer;

public class ParallaxLayer implements Layer {
	
	public final static String LAYER_ID = "Parallax";
	
	private GameServices gameServices;
	private UiContainer container;
	
	private SoundPool soundPool;
	private int soundsLoaded, sfxLaser;
	
	private Image[] image;
	private float speedX[], speedY[];
	private final int AMOUNT = 3;
	
	public ParallaxLayer(GameServices gameServices) {
		this.gameServices = gameServices;
	}

	@Override
	public void onCreate() {
		container = new UiContainer();
		
		image = new Image[AMOUNT];
		speedX = new float[AMOUNT];
		speedY = new float[AMOUNT];
		
		int nro;
		float x, y, size;
		for (int i = 0; i < AMOUNT; i++) {
			speedX[i] = (float)Math.random()+0.1f;
			speedY[i] = (float)Math.random()+0.1f;
			x = (float)Math.random()*Screen.getAspectRatio();
			y = (float)Math.random();
			size = (float)(Math.random()*0.15+0.05);
			nro = (int)(Math.random()*4)+1;
			image[i] = new Image(x, y, size, size,
					gameServices.getTextureManager().getTexture("testi"+nro));
			container.add(image[i]);
		}
		
		// Load sounds
		soundsLoaded = 0;
		soundPool = new SoundPool(12, AudioManager.STREAM_MUSIC, 0);
		soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
			@Override
			public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
				soundsLoaded++;
			}
		});
		sfxLaser = soundPool.load(MainActivity.CONTEXT, R.raw.laser, 1);
		while (soundsLoaded < 1) {
			Thread.yield();
		}
		
	}

	@Override
	public void onOpen() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onUpdate(float dt, ArrayList<InputEvent> inputEvents) {
		
		for (int i = 0; i < AMOUNT; ++i) {
			image[i].setX(image[i].getX()+speedX[i]*dt);
			image[i].setY(image[i].getY()+speedY[i]*dt);
			if (image[i].getX() < 0) {
				image[i].setX(0);
				speedX[i] = Math.abs(speedX[i]);
				soundPool.play(sfxLaser, 0.0f, image[i].getWidth(), 1, 0, 1.0f);
				
			}
			if (image[i].getX() > 1.0f*Screen.getAspectRatio() - image[i].getWidth()) {
				image[i].setX(1.0f*Screen.getAspectRatio() - image[i].getWidth());
				speedX[i] = -speedX[i];
				soundPool.play(sfxLaser, image[i].getWidth(), 0.0f, 1, 0, 1.0f);
			}
			if (image[i].getY() < 0) {
				image[i].setY(0);
				speedY[i] = Math.abs(speedY[i]);
				soundPool.play(sfxLaser, 0.0f, image[i].getWidth(), 1, 0, 1.0f);
			}
			if (image[i].getY() > 1 - image[i].getHeight()) {
				image[i].setY(1 - image[i].getHeight());
				speedY[i] = -speedY[i];
				soundPool.play(sfxLaser, image[i].getWidth(), 0.0f, 1, 0, 1.0f);
			}			
		}

		container.update(dt, inputEvents);
	}

	@Override
	public void onPause() {
		soundPool.autoPause();
	}

	@Override
	public void onResume() {
		soundPool.autoResume();
	}

	@Override
	public void onClose() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onDestroy() {
		soundPool.release();
		container.destroy();
	}

	@Override
	public void draw(RenderList renderList) {
		container.draw(renderList);
	}

	@Override
	public String getId() {
		return LAYER_ID;
	}
 
}

